using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using AjaxControlToolkit;
using System.Web;

[assembly: System.Web.UI.WebResource("OpenPhotoAlbum.Controls.PostPad.js", "text/javascript")]

namespace OpenPhotoAlbum.Controls
{
	[ClientScriptResource("OpenPhotoAlbum.Controls.PostPad", "OpenPhotoAlbum.Controls.PostPad.js")]
    public class PostPad : ScriptControlBase, IPostBackEventHandler
    {
        public const int POST_PAD_WIDTH = 850;
        public const int POST_PAD_HEIGHT = 350;

		public delegate void PostPadEventHandler(object sender, PostPadEventArgs e);
		public event PostPadEventHandler PhotoPosted;

		private Random _random;

        public PostPad()
            : base(HtmlTextWriterTag.Div)
        {
			_random = new Random();
        }

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
		}

		[ExtenderControlEvent]
		public string OnClientPosted { get; set; }

		[ExtenderControlEvent]
		public string OnClientRequestUploading { get; set; }

		[ExtenderControlProperty]
		public string SubmitButtonText { get; set; }

		[ExtenderControlProperty]
		public override string UniqueID { get { return base.UniqueID; } }

        [ExtenderControlProperty]
        public string CancelButtonText { get; set; }

        [ExtenderControlProperty]
        public string WaitText { get; set; }

        [ExtenderControlProperty]
        public string SelectText { get; set; }

        [ExtenderControlProperty]
        public string ChooseLocationText { get; set; }

		[ExtenderControlProperty]
		public int Angle
		{
			get
			{
				int rn = _random.Next(1, 4);
				if (rn == 1) { return -5; }
				else if (rn == 2) { return 0; }
				else { return 5; }
			}
		}

		[ExtenderControlProperty]
		public string CanvasUrl
		{
			get
			{
				//HttpRequest request = HttpContext.Current.Request;
				//string url = string.Format(
				//    System.Globalization.CultureInfo.InvariantCulture,
				//    "{0}://{1}{2}",
				//    request.Url.Scheme,
				//    request.Url.Host,
				//    UrlContext.BuildPhotoPipeUrl(Guid.Empty, BusinessLogics.PhotoTypes.PostPadCanvas));
				//return url;

				return UrlContext.BuildPhotoPipeUrl(Guid.Empty, BusinessLogics.PhotoTypes.PostPadCanvas);
			}
		}

        [ExtenderControlProperty]
        public int PostPadWidth { get { return POST_PAD_WIDTH; } }

        [ExtenderControlProperty]
        public int PostPadHeight { get { return POST_PAD_HEIGHT; } }

		#region IPostBackEventHandler Members

		public void RaisePostBackEvent(string eventArgument)
		{
			if (eventArgument.StartsWith("UploadPostedPhoto"))
			{
				HttpPostedFile file = HttpContext.Current.Request.Files[0];
				byte[] data = new byte[file.ContentLength];
				file.InputStream.Read(data, 0, data.Length);

				string[] args = eventArgument.Split('|');
				PostPadEventArgs e = new PostPadEventArgs()
				{
					OffsetX = int.Parse(args[1]),
					OffsetY = int.Parse(args[2]),
					Angle = int.Parse(args[3]),
					Data = data
				};

				if (PhotoPosted != null) { PhotoPosted(this, e); }
			}
		}

		#endregion
	}

	public class PostPadEventArgs : EventArgs
	{
		public int OffsetX { get; set; }
		public int OffsetY { get; set; }
		public int Angle { get; set; }
		public byte[] Data { get; set; }
	}
}
